package com.rtx.edi.platform.application.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rtx.edi.model.application.po.PlApplicationField;
import com.rtx.edi.platform.application.mapper.PlApplicationFieldMapper;
import com.rtx.edi.platform.application.service.PlApplicationFieldService;

/**
 * <p>
 * 应用字段信息表  服务实现类
 * </p>
 *
 * @author wq
 * @since 2023-03-16
 */
@Service
public class PlApplicationFieldServiceImpl extends ServiceImpl<PlApplicationFieldMapper, PlApplicationField> implements PlApplicationFieldService {

    @Override
    public void selectPlApplicationFieldList(Page<PlApplicationField> pageParam, PlApplicationField po) {
        LambdaQueryWrapper<PlApplicationField> query = Wrappers.lambdaQuery();
        query.eq(po.getApplicationNo() != null,PlApplicationField::getApplicationNo, po.getApplicationNo())
                .like(po.getApplicationName() != null, PlApplicationField::getApplicationName, po.getApplicationName())
                .eq(po.getFieldCode() != null, PlApplicationField::getFieldCode, po.getFieldCode())
                .like(po.getFieldName() != null, PlApplicationField::getFieldName, po.getFieldName())
                .orderByAsc(PlApplicationField::getApplicationNo,PlApplicationField::getFieldCode);
        baseMapper.selectPage(pageParam,query);
    }

    @Override
    public int addPlApplicationField(PlApplicationField po) {
        return insertPlApplicationField(po);
    }

    @Override
    public int editPlApplicationField(PlApplicationField po) {
        return updatePlApplicationField(po);
    }

    /**
     * 写入应用字段信息表
     * @param po
     */
    private int insertPlApplicationField(PlApplicationField po) {
        //验证应用编号、字段code
        checkFieldCode(po);
        //验证应用编号，字段名称
        checkFieldName(po);
        return baseMapper.insert(po);
    }

    /**
     * 更新应用字段信息表
     * @param po
     */
    private int updatePlApplicationField(PlApplicationField po) {
        //验证是否存在
        PlApplicationField pl = baseMapper.selectById(po.getId());
        Assert.notNull(pl,"应用字段信息id：" + po.getId() + "不存在");
        //补全应用编号，字段编码，字段名称
        if(!StringUtils.hasText(po.getApplicationNo())){
            po.setApplicationNo(pl.getApplicationNo());
        }
        if(!StringUtils.hasText(po.getFieldCode())){
            po.setFieldCode(pl.getFieldCode());
        }
        if(!StringUtils.hasText(po.getFieldName())){
            po.setFieldName(pl.getFieldName());
        }
        //如果修改的应用编号，字段编码与数据库不一致，则验证
        if(!po.getApplicationNo().equals(pl.getApplicationNo()) ||
                !po.getFieldCode().equals(pl.getFieldCode()) ){
            checkFieldCode(po);
        }
        //如果修改的应用编号，字段名称与数据库不一致，则验证
        if(!po.getApplicationNo().equals(pl.getApplicationNo()) ||
                !po.getFieldName().equals(pl.getFieldName())){
            checkFieldName(po);
        }
        return baseMapper.updateById(po);
    }

    /**
     * 验证应用编号、字段code
     * @param po
     */
    private void checkFieldCode(PlApplicationField po){
        //根据应用编号、字段code查询是否存在
        LambdaQueryWrapper<PlApplicationField> query = Wrappers.lambdaQuery();
        query.eq(PlApplicationField::getApplicationNo,po.getApplicationNo())
                .eq(PlApplicationField::getFieldCode,po.getFieldCode());
        Long count1 = baseMapper.selectCount(query);
        Assert.isTrue(count1 < 1,"应用编号、字段code不允许重复");
    }

    /**
     * 验证应用编号，字段名称
     * @param po
     */
    private void checkFieldName(PlApplicationField po){
        //根据应用编号、字段名称查询是否存在
        LambdaQueryWrapper<PlApplicationField> query = Wrappers.lambdaQuery();
        query.eq(PlApplicationField::getApplicationNo,po.getApplicationNo())
                .eq(PlApplicationField::getFieldName,po.getFieldName());
        Long count2 = baseMapper.selectCount(query);
        Assert.isTrue(count2 < 1,"应用编号、字段名称不允许重复");
    }
}
